{% extends "layout/basic.html" %}
{% import "components/comments_discussion.html" as comments with context %}
{% block content %}
<div class="row">
  <div class="medium-9 columns">
    <div class="section">
      <div class="section__body">
        <ul class="supplementary list">
          <li>
            <a class="discussion-node-tag" href="{{ url('discussion_node', type=model.discussion.typeDisplay[vnode.type], name=vnode.id) }}">
            {% if ddoc.parentType == model.document.TYPE_DISCUSSION_NODE %}
              <span class="v-center icon icon-tag"></span>
            {% elif ddoc.parentType == model.document.TYPE_CONTEST %}
              <span class="v-center icon icon-award"></span>
            {% else %}
              <span class="v-center icon icon-flag"></span>
            {% endif %}
              {{ vnode['title'] }}
            </a>
          </li>
          <li><h2>{{ ddoc.title }}</h2></li>
          <li>
            {{ user.render_inline(udict[ddoc.owner]) }}
            @
            {{ datetimeSpan(ddoc._id)|safe }}
          </li>
        </ul>
        <div class="typo topic__content richmedia" data-emoji-enabled data-raw-url="{{ url('discussion_raw', did=ddoc.docId) }}">
          {{ ddoc['content']|markdown|safe }}
        </div>
      </div>
      <ul class="section__footer supplementary dot list">
      {% if handler.user.hasPerm(perm.PERM_EDIT_DISCUSSION) or (handler.user.own(ddoc) and handler.user.hasPerm(perm.PERM_EDIT_DISCUSSION_SELF)) %}
        <li><a href="{{ url('discussion_edit', did=ddoc.docId) }}">
          <span class="icon icon-edit"></span> {{ _('Edit') }}
        </a></li>
      {% endif %}
      {% if ddoc.edited %}
        <li data-discussion-history data-raw-url="{{ url('discussion_raw', did=ddoc.docId) }}">{{ _('Edited') }}</li>
      {% endif %}
        <li>{{ _('{0} views').format(ddoc.views) }}</li>
      {% if handler.user.hasPriv(PRIV.PRIV_USER_PROFILE) %}
        <li><form class="form--inline" method="post">
          <input type="hidden" name="operation" value="{% if not dsdoc['star'] %}star{% else %}unstar{% endif %}">
          <button class="star{% if dsdoc['star'] %} activated{% endif %}" type="submit">
            <span class="starred--hide"><span class="icon icon-star--outline"></span> {{ _('Star Topic') }}</span>
            <span class="starred--show"><span class="icon icon-star"></span> {{ _('Unstar Topic') }}</span>
          </button>
        </form></li>
      {% endif %}
      {% if handler.user.own(ddoc) or handler.user.hasPerm(perm.PERM_LOCK_DISCUSSION) %}
        <li><form class="form--inline" method="post">
          <input type="hidden" name="operation" value="set_lock">
          {% if not ddoc.lock %}<input type="hidden" name="lock" value="1">{% endif %}
          <button class="star{% if ddoc.lock %} activated{% endif %}" type="submit">
            <span class="starred--hide">{{ _('Lock Topic') }}</span>
            <span class="starred--show">{{ _('Unlock Topic') }}</span>
          </button>
        </form></li>
      {% endif %}
        <li><a href="{{ url('wiki_help', anchor='contact') }}">
          <span class="icon icon-warning"></span> {{ _('Report') }}
        </a></li>
      {% if handler.user.hasPerm(perm.PERM_ADD_REACTION) %}
        <li><a href="javascript:;" data-op="react" data-type="discuss" data-form="{{ {operation: 'reaction', nodeType:'did', id: ddoc._id }|json }}">
          <span class="icon icon-emoji"></span>
        </a></li>
      {% endif %}
      <div class="reactions list" data-type="did" data-did="{{ ddoc._id }}">
        {% for e in Object.entries(ddoc.react or {})|sort(true, false, 1)|selectattr(1) %}
          <div class="reaction{% if reactions[ddoc.docId][e[0]] %} active{% endif %}"><span class="emoji">{{ e[0] }}</span> {{ e[1] }}</div>
        {% endfor %}
      </div>
      </ul>
    </div>
    <div class="section">
      <div class="section__header">
        <h1 class="section__title">{{ _('{0} comments').format(drcount) }}</h1>
      </div>
      <div class="section__body">
        {{ comments.render(
          view = 'discussion',
          docs = drdocs,
          udict = udict,
          comment_ref = 'drid',
          reply_ref = 'drrid',
          comment_post_op = 'reply',
          reply_post_op = 'tail_reply',
          comment_edit_op = 'edit_reply',
          comment_delete_op = 'delete_reply',
          reply_edit_op = 'edit_tail_reply',
          reply_delete_op = 'delete_tail_reply',
          comment_post_perm = perm.PERM_REPLY_DISCUSSION,
          comment_delete_perm = perm.PERM_DELETE_DISCUSSION_REPLY_SELF_DISCUSSION if handler.user.own(ddoc) else perm.PERM_DELETE_DISCUSSION_REPLY,
          comment_edit_self_perm = perm.PERM_EDIT_DISCUSSION_REPLY_SELF,
          comment_delete_self_perm = perm.PERM_DELETE_DISCUSSION_REPLY_SELF,
          reply_post_perm = perm.PERM_REPLY_DISCUSSION,
          reply_delete_perm = perm.PERM_DELETE_DISCUSSION_REPLY,
          reply_edit_self_perm = perm.PERM_EDIT_DISCUSSION_REPLY_SELF,
          reply_delete_self_perm = perm.PERM_DELETE_DISCUSSION_REPLY_SELF,
          reactions = reactions,
          ddoc = ddoc
        ) }}
        {{ paginator.render(page, pcount) }}
      {% if drcount == 0 %}
        {{ nothing.render('No comments so far...') }}
      {% endif %}
      </div>
    </div>
  </div>
  <div class="medium-3 columns">
    {% set udoc = udict[ddoc.owner] %}
    <div class="section side">
      <div class="profile__bg user-profile-bg--1"></div>
      <div class="section__body">
        <div class="media sidebar-user-profile">
          <div class="media__left">
            <img src="{{ avatarUrl(udoc.avatar, 80) }}" width="80" height="80" class="medium user-profile-avatar" loading="lazy">
          </div>
          <div class="media__body">
            <p>{{ user.render_inline(udoc, avatar=false, modbadge=false) }}</p>
          </div>
        </div>
      </div>
      <div class="section__body">
        <div class="balancer sidebar-user-stat">
          <div class="balancer__body">
            <div class="numbox">
              <div class="numbox__num medium">{{ udoc.nAccept|default(0) }}</div>
              <div class="numbox__text">{{ _('Accepted') }}</div>
            </div>
          </div>
          <div class="balancer__body">
            <div class="numbox">
              <div class="numbox__num medium">{{ udoc.nLike|default(0) }}</div>
              <div class="numbox__text">{{ _('Solutions Liked') }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    {% if vnode.type == model.document.TYPE_PROBLEM %}
      {% set pdoc = vnode %}
      {% set owner_udoc = udict[vnode.owner] %}
      {% include "partials/problem_sidebar.html" %}
    {% endif %}
  </div>
</div>
{% endblock %}
